<?php

/**
 * 供应商店铺
 *
 * Author: netman
 * DateTime:  2023/10/16 10:37
 */
class Service_Api_Supplier_Shop extends Contract_Api_Supplier_Shop
{

    use Trait_Api_Common;

    /**
     * 获取热门产品
     * @param $channel
     * @param $supplier_id
     * @return mixed
     * User: netman
     * Date: 2023/10/17 16:31
     */
    private function get_hot_product($channel, $supplier_id)
    {
        foreach ($channel as &$v) {
            $pinyin = $v['pinyin'];
            $params = array(
                'page' => 1,
                'pagesize' => 4,
                'supplierid' => $supplier_id,
                'table' => $v['table'] ? $v['table'] : ''
            );
            if (in_array($v['type_id'], array(1, 2, 3, 5, 11, 13, 114, 118, 121))) {
                $func = 'search_' . $pinyin . '_list';
            } else {
                if ($v['type_id'] > 199) {
                    $func              = 'search_tongyong_list';
                    $params['type_id'] = $v['type_id'];
                }
            }
//            $func = 'search_food_list';
            $out = Model_Supplier_Product::$func($params);
            foreach ($out['list'] as &$l) {
                $l['litpic'] = $this->img_url($l['litpic']);
                $l['typeid'] = $v['type_id'];
            }
            $v['list'] = $out['list'];
        }

        return $channel;
    }

    /**
     * 获取店铺配置
     * @param $supplier_id
     * @return array
     * @throws Exception
     * User: netman
     * Date: 2023/10/17 16:47
     */
    function get_shop_config($supplier_id): array
    {
        $supplier = DB::select()->from('supplier')->where('id', '=', $supplier_id)->execute()->current();
        if (!$supplier) {
            throw new Exception('查询供应商店铺失败');
        }
        $supplier['qualification'] = unserialize($supplier['qualification']);
        $shops                     = DB::select()->from('supplier_shop')->where('supplier_id', '=', $supplier_id)->execute()->current();
        $shops['logo']             = $this->img_url($shops['logo']);
        $shops['title']            = empty($shops['title']) ? $supplier['suppliername'] : $shops['title'];
        $shops['litpic']           = $this->img_url($supplier['litpic']);
        $shops['content']          = St_Functions::cutstr_html($supplier['content'], 10);

        //经验范围
        $modelname = DB::select('id', 'modulename', 'maintable',
            'pinyin')->from('model')->where(DB::expr("id in({$supplier['authorization']})"))->execute()->as_array();
        $channel   = Model_Supplier_Shop::get_supplier_has_channels($supplier_id);
        //交集
        foreach ($channel as $k => $v) {
            foreach ($modelname as $k1 => $v1) {
                if ($v['type_id'] == $v1['id']) {
                    $arr[$k] = $v;
                }
            }
        }
        //$this->_channel = $arr;

        if ($supplier['qualification']['mp_litpic']) {
            $supplier['qualification']['mp_litpic'] = $this->img_url($supplier['qualification']['mp_litpic']);
        }

        // 是否显示联系方式配置
        $show_contract = Model_Sysconfig::get_configs('0', 'cfg_supplier_contract_status', 1);
        // 获取热门产品
        $hot_product = $this->get_hot_product($arr, $supplier_id);

        $data = array(
            'supplier' => $supplier,
            'shops' => $shops,
            'show_contract' => $show_contract,
            'channel' => $arr,
            'hot_product' => $hot_product,
        );
        return $data;
    }


    /**
     * 获取产品列表
     * @param $params
     * @return array
     * @throws Exception
     * User: netman
     * Date: 2023/10/17 16:47
     */
    function get_product_list($params): array
    {
        $page        = $params['page'];
        $page        = $page < 1 ? 1 : $page;
        $pagesize    = $params['pagesize'];
        $pagesize    = $pagesize < 1 ? 10 : $pagesize;
        $supplier_id = $params['supplier_id'];
        $pinyin      = $params['pinyin'];
        $type_id     = $params['type_id'];
        $table       = $params['table'];
        $supplier    = Model_Supplier::get_supplier_info($supplier_id);
        if (!$pinyin || !$supplier_id || $supplier['verifystatus'] != 3) {
            throw new Exception("查询供应商店铺失败");
        }
        $params = array(
            'page' => $page,
            'pagesize' => $pagesize,
            'supplierid' => $supplier_id,
            'table' => $table ? $table : ''
        );
        if (isset($params['table']) && $params['table'] == 'model_archive') {
            $func              = 'search_tongyong_list';
            $params['type_id'] = $type_id;
        } else {
            $func = 'search_' . $pinyin . '_list';
        }

        $out = Model_Supplier_Product::$func($params);
        count($out['list']) < $pagesize ? $page = -1 : ++$page;
        foreach ($out['list'] as &$l) {
            $l['litpic'] = $this->img_url($l['litpic']);
            $l['typeid'] = $type_id;
        }
        return $out;
    }


    /**
     * 获取供应商店铺信息
     * @param $supplier_id
     * @return array|mixed|string
     */
    public function get_shop_info($supplier_id)
    {
        $supplier = '';
        if (!$supplier_id) {
            return '';
        }
        if (Model_Supplier::display_is_open()) {
            $supplier = DB::select('id', 'suppliername', 'litpic')
                ->from('supplier')
                ->where('id', '=', $supplier_id)
                ->and_where('verifystatus', '=', 3)
                ->execute()
                ->current();
            if ($supplier) {
                if (St_Functions::is_normal_app_install('supplier_shop')) {
                    $shop               = Model_Supplier_Shop::get_supplier_shop($supplier_id);
                    $supplier['litpic'] = $this->img_url($shop['logo']);
                } else {
                    $supplier['litpic'] = $this->img_url($supplier['litpic']);
                }
            }
        }
        return $supplier;
    }

}